from django.contrib import admin
from django.utils.safestring import mark_safe

from casedemo5.models import BookClass,BookInfo     ##BookClass,BookInfo模型的管理器

class BookClassAdmin(admin.ModelAdmin):
    list_display=["id","name"]

class BookInfoAdmin(admin.ModelAdmin):
    ## 显示图片
    def buttons(self, obj):
        button_html = """<a class="changelink"href="http://127.0.0.1:8000/admin/casedemo5/bookinfo/%s/change/">编辑</a>""" % (
            obj.id)
        return mark_safe(button_html)
    buttons.short_description = '图片'
    ##显示图片
    def booking(self, obj):
        print(obj.imgsrc)
        if obj.imgsrc:
            return mark_safe(f'<img src="{obj.imgsrc.url}" width="40px" height="40px" />')
        else:
            return "无图片"
    booking.short_description = "图书封面"
    ## list_editable设置列表中可编辑字段
    # list_editable = ('name', 'autor')
    ##search_fields设置可搜索字段
    # search_fields = ('name', 'autor')
    ##ordering设置排序方式；-：降序
    # ordering = ['-price']
    list_display = ["id", "name", "price", "autor", "bookclass", "buttons", "booking"]  ##设置显示的字段

##在admin中注册绑定
admin.site.register(BookClass,BookClassAdmin)
admin.site.register(BookInfo,BookInfoAdmin)
##在应用下的admin添加
admin.site.site_header='django后台的登录系统'
admin.site.site_title='django后台管理系统'





